#!/bin/bash

echo "admin服务重启开始............."
SERVER_JAR_NAME="/data/cloud/nb_admin.jar"

# 查找正在运行的进程ID
ID=$(ps -ef | grep "$SERVER_JAR_NAME" | grep -v "grep" | awk '{print $2}')

if [ -z "$ID" ]; then
  echo "未找到运行中的admin服务，跳过杀进程步骤。"
else
  echo "找到运行中的admin服务，正在终止......"
  for id in $ID; do
    kill -9 $id
    echo "已终止进程 $id"
  done
fi

# 设置日志文件带时间戳
LOG_DIR="./logs"
mkdir -p $LOG_DIR
LOG_FILE="$LOG_DIR/admin_.out"

echo "重启服务中............."
nohup java -Xms256m -Xmx1024m -Xmn128m \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=100 \
-XX:+UseStringDeduplication \
-XX:NewRatio=3 \
-XX:SurvivorRatio=8 \
-XX:MaxMetaspaceSize=128m \
-Dfile.encoding=UTF-8 -jar $SERVER_JAR_NAME > $LOG_FILE 2>&1 &

sleep 3  # 确保进程已启动
NEW_ID=$(ps -ef | grep "$SERVER_JAR_NAME" | grep -v "grep" | awk '{print $2}')
if [ -z "$NEW_ID" ]; then
  echo "admin服务启动失败，请检查日志：$LOG_FILE"
else
  echo "admin服务重启成功，PID: $NEW_ID"
fi
